#!/usr/bin/env python

import sys, os
import controller, getch


if len(sys.argv) != 4:
    print "================================================================================"
    print " "*4 + "Debes llamarme de esta manera:"
    print " "*4 + sys.argv[0] + " <datos> <puerto_serial> <baudaje>\n"
    print " "*4 + "Ejemplo: " + sys.argv[0] + " data.txt /dev/ttyUSB0 4800"
    print "\n\n ttyX para los puertos serial \n ttyUSBX para los puerto serial convertidos de USB"
    print "\n\n Donde X es un entero"
    print "================================================================================"
    sys.exit(1)
    
# vars globals
so = os.name
run = True

ruta = sys.argv[0]
tmp = ruta.split("/")
tmp.pop()
if tmp[0] == ".":
    tmp.pop(0)
ruta = "/".join(tmp)

# Clear console
if so == "posix":
    command = "clear"
elif so == "nt":
    command = "cls"    

os.system(command)
print "--- INICIANDO ---"

machine = controller.Machine(table = ruta + str(sys.argv[1]),port = str(sys.argv[2]), baudrate = int(sys.argv[3]))

if machine.error:
    print machine.merror
    sys.exit(1)
else:
    print "--- LISTO PARA USAR ---"

while run:
    key = getch.getch()
    
    if key == 'q':
        run = False
        print key + " --- AFUERA !! Vamos cerrando.."
        print " --- CERRANDO ---"
        sys.exit(0)
    else:
        tuple = machine.play_sound(key)
